package com.zzw.springboot.util;

import com.zzw.springboot.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * RabbitMQ工具类
 */
@Component
public class RabbitMQUtil {
    
    @Autowired
    private RabbitAdmin rabbitAdmin;
    
    /**
     * 获取队列消息数量
     * 
     * @param queueName 队列名称
     * @return 消息数量，如果队列不存在返回-1
     */
    public int getQueueMessageCount(String queueName) {
        Properties properties = rabbitAdmin.getQueueProperties(queueName);
        if (properties != null && properties.containsKey("QUEUE_MESSAGE_COUNT")) {
            return (int) properties.get("QUEUE_MESSAGE_COUNT");
        }
        return -1;
    }
    
    /**
     * 检查队列是否存在
     * 
     * @param queueName 队列名称
     * @return 是否存在
     */
    public boolean queueExists(String queueName) {
        return rabbitAdmin.getQueueProperties(queueName) != null;
    }
    
    /**
     * 清除队列中的所有消息
     * 
     * @param queueName 队列名称
     * @return 是否成功
     */
    public boolean purgeQueue(String queueName) {
        try {
            rabbitAdmin.purgeQueue(queueName, false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
} 